package com.zk.iotgateway.controller;


import com.zk.iotgateway.domain.IotJob;
import com.zk.iotgateway.domain.Msg;
import com.zk.iotgateway.quartz.JobTask;
import com.zk.iotgateway.service.JobService;
import com.zk.iotgateway.util.JsonUtil;
import com.zk.iotgateway.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**
 * 定时任务 controller
 */
@Controller
@RequestMapping("/job")
public class JobController{

    @Autowired
    JobService jobService;

    @Autowired
    JobTask jobTask;

    @GetMapping(value = "showJob")
    public String showJob(Model model) {
        return "job/jobList";
    }

    @GetMapping(value = "showJobList")
    @ResponseBody
    public Msg showJobList() {
        List<IotJob> list = jobService.selectAll();
        return ResultUtil.success(list);
    }

    @GetMapping(value = "showAddJob")
    public String addJob(Model model) {
        return "job/add-job";
    }


    @PostMapping(value = "addJob")
    @ResponseBody
    public JsonUtil addJob(IotJob job) {
        JsonUtil j = new JsonUtil();
        String msg = "保存成功";
        job.setId(UUID.randomUUID().toString());
        job.setStatus("0");
        try {
            jobService.insertSelective(job);
        } catch (Exception e) {
            msg = "保存失败";
            j.setFlag(false);
            e.printStackTrace();
        }
        j.setMsg(msg);
        return j;
    }

    @GetMapping(value = "updateJob")
    public String updateJob(String id, Model model, boolean detail) {
        if (StringUtils.isNotEmpty(id)) {
            IotJob job = jobService.selectByPrimaryKey(id);
            model.addAttribute("job", job);
        }
        model.addAttribute("detail", detail);
        return "job/update-job";
    }



    @PostMapping(value = "editJob")
    @ResponseBody
    public JsonUtil updateJob(IotJob job) {
        JsonUtil j = new JsonUtil();
        j.setFlag(false);
        if (job == null) {
            j.setMsg("获取数据失败");
            return j;
        }
        if (jobTask.checkJob(job)) {
            j.setMsg("已经启动任务无法更新,请停止后更新");
            return j;
        }
        if (jobService.updateJob(job)) {
            j.setFlag(true);
            j.setData("更新成功");
        } else {
            j.setData("更新失败");
        }
        return j;
    }


    @PostMapping(value = "del")
    @ResponseBody
    public JsonUtil del(String id) {
        return jobService.del(id);
    }



    @PostMapping(value = "startJob")
    @ResponseBody
    public JsonUtil startJob(String id) {
        JsonUtil j = new JsonUtil();
        String msg = null;
        if (StringUtils.isEmpty(id)) {
            j.setMsg("获取数据失败");
            j.setFlag(false);
            return j;
        }
        if (jobService.startJob(id)) {
            msg = "启动成功";
        } else {
            msg = "启动失败";
        }
        j.setMsg(msg);
        return j;
    }


    @PostMapping(value = "endJob")
    @ResponseBody
    public JsonUtil endJob(String id) {
        JsonUtil j = new JsonUtil();
        String msg;
        if (StringUtils.isEmpty(id)) {
            j.setMsg("获取数据失败");
            j.setFlag(false);
            return j;
        }
        if (jobService.stopJob(id)) {
            msg = "停止成功";
        } else {
            msg = "停止失败";
        }
        j.setMsg(msg);
        return j;
    }

}
